﻿using Frame;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace VideoFileOrganization.OrganizesubFiles
{
    public partial class OrganizesubFilesFrm : BaseFrm
    {
        public OrganizesubFilesFrm()
        {
            InitializeComponent();
            this.Text = "子文件整理";
            this.Index = 4;
            this.TabImage = Properties.Resources.submenu;
            dgSubDir.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        protected override void IniUi()
        {
            dgSubDir.Rows.Clear();
            // 获取目录对象
            DirectoryInfo directory = new DirectoryInfo(RootPath);
            // 获取子文件夹
            DirectoryInfo[] subDirectories = directory.GetDirectories();

            foreach (DirectoryInfo dir in subDirectories)
            {
                // 获取子文件夹
                DirectoryInfo[] subs = dir.GetDirectories();
                // 获取子文件
                string[] subfiles = Directory.GetFiles(dir.FullName);
                //如果没有子文件夹就跳过
                if (subfiles.Count() <= 0) continue;
                dgSubDir.Rows.Add();
                int row = dgSubDir.RowCount - 1;
                dgSubDir.Rows[row].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                dgSubDir.Rows[row].Cells[0].Value = true;
                dgSubDir.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgSubDir.Rows[row].Cells[1].Value = Path.GetFileName(dir.Name);
                dgSubDir.Rows[row].Cells[2].Value = dir.FullName;
            }
        }

        protected override void ApplyButton()
        {
            for (int i = 0; i < dgSubDir.RowCount; i++)
            {
                object cellValue = dgSubDir.Rows[i].Cells[0].Value;
                if (cellValue != null && cellValue.Equals(true))
                {
                    string path = dgSubDir.Rows[i].Cells[2].Value.ToString();
                    ParameterizedThreadStart thread = new ParameterizedThreadStart(OrganizationThread);
                    Thread th = new Thread(thread);
                    th.Start(path);
                    th.Join();
                }
                else
                {
                    continue;
                }
            }
            Invoke(new IniUiDelegate(IniUi));

        }




        // 在线程中操作 更新界面的委托
        private delegate void IniUiDelegate();
        private void OrganizationThread(Object obj)
        {
            string _rootPath = (string)obj;
            //string[] files = Directory.GetFiles(_rootPath);
            //Debug.Log(files.Count());
            //try
            //{
            //    foreach (string file in files)
            //    {
            //        // 获取当前目录的父目录
            //        string parentDirectory = Directory.GetParent(_rootPath).FullName;
            //        //Debug.Log(parentDirectory);
            //        string newPath = DirectoryHelper.Rename(parentDirectory, file);
            //        //Debug.Log(newPath);
            //        try
            //        {
            //            if (File.Exists(newPath))
            //            {
            //                int count = 1;
            //                string originalFileName = Path.GetFileNameWithoutExtension(newPath);
            //                string extension = Path.GetExtension(newPath);

            //                while (File.Exists(newPath))
            //                {
            //                    newPath = Path.Combine(_rootPath, $"{originalFileName} ({count}){extension}");
            //                    count++;
            //                }
            //            }

            //            File.Move(file, newPath);
            //        }
            //        catch (Exception ex)
            //        {
            //            SendMsg(newPath + "[" + ex.ToString() + "]", LogLevel.ERROR);
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}


            // 获取当前目录的父目录
            string parentDirectory = Directory.GetParent(_rootPath).FullName;
            Debug.Log(_rootPath);
            DirectoryHelper.MoveFiles(_rootPath, parentDirectory);
            
            //删除空白文件夹
            DirectoryHelper.DeleteEmptyFolders(parentDirectory);
        }

        
    }
}
